﻿using System;
using Techappy.Silverlight.Toolkit.Primitives;
using System.Collections.Generic;
using System.Windows.Controls;

namespace ToolkitSamples.Views
{
    public class LoopingSelectorDataSource : ILoopingSelectorDataSource
    {
        public List<string> Items;

        public LoopingSelectorDataSource()
        {
            Items = new List<string>();
            Items.Add("全部");
            Items.Add("最新");
            Items.Add("最热");
            Items.Add("爱情");
            Items.Add("文艺");

            Items.Add("其他");

            this.SelectedItem = Items[3];
        }

        public object GetNext(object relativeTo)
        {
            if (relativeTo == null) return null;
            int index = Items.IndexOf(relativeTo.ToString());

            if (index == Items.Count - 1) return null;
            //if (index == Items.Count - 1) return Items[0];
            return Items[index + 1];
        }

        public object GetPrevious(object relativeTo)
        {
            if (relativeTo == null) return null;
            int index = Items.IndexOf(relativeTo.ToString());

            if (index == 0) return null;
            //if (index == 0) return Items[Items.Count - 1];
            return Items[index - 1];
        }

        public object _selectedItem;
        public object SelectedItem
        {
            get
            {
                return _selectedItem;
            }
            set
            {
                if (!object.Equals(this._selectedItem, value))
                {
                    if (SelectionChanged != null)
                        SelectionChanged(this, new SelectionChangedEventArgs(new object[] { _selectedItem }, new object[] { value }));

                    _selectedItem = value;
                }
            }
        }

        public event EventHandler<SelectionChangedEventArgs> SelectionChanged;
    }
}
